#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2018/11/12 10:22
# @Author  : yangdy
# @Email   : yangdy@egu360.com
# @File    : 中级-Insert Delete GetRandom O(1).py
# @Software: PyCharm


class RandomizedSet:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.__m = {}

    def insert(self, val):
        """
        Inserts a value to the set. Returns true if the set did not already contain the specified element.
        :type val: int
        :rtype: bool
        """
        if not self.__m.get(val):
            self.__m[val] = 1
            return True
        else:
            return False

    def remove(self, val):
        """
        Removes a value from the set. Returns true if the set contained the specified element.
        :type val: int
        :rtype: bool
        """
        if not self.__m.get(val):
            return False
        else:
            # self.__m[val] = 0
            self.__m.pop(val)
            return True

    def getRandom(self):
        """
        Get a random element from the set.
        :rtype: int
        """
        from random import randint
        keys = self.__m.keys()
        i = randint(0, len(keys)-1)
        key = list(keys)[i]
        return key


# Your RandomizedSet object will be instantiated and called as such:
# obj = RandomizedSet()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()

def op1():
    obj = RandomizedSet()
    p1 = obj.insert(1)
    print('insert 1:', p1)
    p2 = obj.insert(2)
    print('insert 2:', p2)
    p1 = obj.insert(1)
    print('insert 1:', p1)
    p1 = obj.remove(1)
    print('remove 1:', p1)
    p1 = obj.insert(1)
    print('insert 1:', p1)
    p2 = obj.insert(2)
    print('insert 2:', p2)
    p3 = obj.insert(3)
    print('insert 3:', p3)
    print('get random:', obj.getRandom())


def op2():
    p0 = obj = RandomizedSet()
    print(p0)
    p1 = obj.insert(1)
    print(p1)
    p2 = obj.remove(2)
    print(p2)
    p3 = obj.insert(2)
    print(p3)
    p4 = obj.getRandom()
    print(p4)
    p5 = obj.remove(1)
    print(p5)
    p6 = obj.insert(2)
    print(p6)
    p7 = obj.getRandom()
    print(p7)



def main():
    op2()

if __name__ == '__main__':
    main()
